package db_main

import (
	"errors"
	"fmt"
	"gorm.io/gorm"
	"longmen/server/config/global"
	"longmen/server/pkg/common/helper/util"
	"longmen/server/pkg/db/mysql"
	"longmen/server/pkg/db/mysql/models/models_main"
)

func GetGroupChatUserList(param map[string]interface{}, order string, page, size int) ([]*models_main.GroupChatUser, error) {
	fName := "GetGroupChatUserList"
	o := make([]*models_main.GroupChatUser, 0)
	table := mysql.GetRTable(&models_main.GroupChatUser{})
	mysql.Condition(table, param, "", order, page, size)
	err := table.Find(&o).Error
	if err != nil {
		err = fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	return o, err
}

func GetGroupChatUserJoinList(param map[string]interface{}, order string, page, size int) ([]*models_main.GroupChatUser, error) {
	fName := "GetGroupChatUserJoinList"
	o := make([]*models_main.GroupChatUser, 0)
	table := mysql.GetRTable(&models_main.GroupChatUser{})
	mysql.Condition(table, param, "", order, page, size)
	err := table.Joins("LEFT JOIN main.db_user u on u.id = user_id").Find(&o).Error
	if err != nil {
		err = fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	return o, err
}

func GetGroupChatUser(param map[string]interface{}) (*models_main.GroupChatUser, error) {
	fName := "GetGroupChatUser"
	o := &models_main.GroupChatUser{}
	table := mysql.GetRTable(&models_main.GroupChatUser{})
	mysql.Condition(table, param, "", "id desc", 0, 0)
	err := table.Take(o).Error
	if errors.Is(err, gorm.ErrRecordNotFound) {
		return o, nil
	}
	if err != nil {
		err = fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	return o, err
}

func InsertGroupChatUser(GroupChatUser *models_main.GroupChatUser) error {
	fName := "InsertGroupChatUser"
	err := global.GetX().Create(GroupChatUser).Error
	if err != nil {
		return fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	return nil
}

func UpdateGroupChatUser(columns []string, GroupChatUser *models_main.GroupChatUser) error {
	fName := "UpdateGroupChatUser"
	err := global.GetX().
		Model(GroupChatUser).
		Select(columns).
		Updates(GroupChatUser).Error
	if err != nil {
		return fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	return nil
}

func UpdateGroupChatUsersRoleAdmin(ids []int, role int) error {
	fName := "UpdateGroupChatUsersRoleAdmin"
	err := global.GetX().
		Model(&models_main.GroupChatUser{}).
		Where("id in ?", ids).
		UpdateColumn("role", role).Error
	if err != nil {
		return fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	return nil
}

func DeleteGroupChatUser(GroupChatUser *models_main.GroupChatUser) error {
	fName := "DeleteGroupChatUser"
	err := global.GetX().Delete(GroupChatUser, GroupChatUser.Id).Error
	if err != nil {
		return fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	return nil
}

func DeleteGroupChatUsers(ids []int) error {
	fName := "DeleteGroupChatUsers"
	err := global.GetX().Delete(&models_main.GroupChatUser{}, "id in ?", ids).Error
	if err != nil {
		return fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	return nil
}
